package main.com.dao;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @author lk
 * @date 2020/6/23 11:16
 * @description: 连接数据库，提供基本方法
 */
public class BaseDao {

    protected static final String CLASS_NAME = "com.mysql.jdbc.Driver";
    protected static final String URL = "jdbc:mysql://127.0.0.1:3306/course?characterEncoding=UTF-8";
    protected static final String USERNAME = "root";
    protected static final String PASSWORD = "123456";

    /**
     * 建立连接
     *
     * @return
     */
    protected Connection getConnection() {
        Connection c = null;
        try {
            Class.forName(CLASS_NAME);
            c = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            return c;
        } catch (Exception ex) {
            ex.printStackTrace();
            return c;
        }
    }

    /**
     * 关闭连接
     *
     * @param c
     */
    protected void closeConnection(Connection c) {
        try {
            if (c != null) {
                c.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
